package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.GoodsCategory;
import com.dingreading.cloud.admin.service.GoodsCategoryLogService;
import com.dingreading.cloud.admin.service.GoodsCategoryService;
import com.dingreading.cloud.admin.service.GoodsService;
import com.dingreading.cloud.admin.util.redis.RedisService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.RedisParam;
import com.google.common.collect.Lists;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/system/goodsCategory")
@Api(tags = "总部-商品种类管理")
public class GoodsCategoryController {

    @Resource
    protected GoodsCategoryService goodsCategoryService;
    @Resource
    protected GoodsCategoryLogService goodsCategoryLogService;
    @Resource
    protected RedisService redisService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "种类名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "商城展示状态 1.是 0.否") @RequestParam(value = "mallStatus", required = false) Integer mallStatus,
            @ApiParam(value = "是否禁用 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<GoodsCategory> pageList = goodsCategoryService.pageList(pageUtil, name, mallStatus, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(pageList.getTotalRow());
        data.setList(pageList.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "获取所有商品种类")
    @PostMapping(value = "/getAll", produces = "application/json; charset=UTF-8")
    public R<Object> getAll(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName
    ) {
        List<GoodsCategory> list = goodsCategoryService.getList();
        return R.ok(list);
    }


    @ApiOperation(value = "添加/编辑商品种类")
    @PostMapping(value = "/editBean", produces = "application/json; charset=UTF-8")
    public R<Object> editBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "商品种类id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "种类名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "商城展示状态 1.是 0.否") @RequestParam(value = "mallStatus", required = false) Integer mallStatus,
            @ApiParam(value = "状态 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        GoodsCategory data = null;
        if (id == null || id <= 0) {
            // 新增
            boolean cnt = goodsCategoryService.getSameNameCnt(null, name);
            if (cnt) return R.fail("种类名称已存在");
            // 获取最大序号
            int maxOrderNum = goodsCategoryService.getMaxShowOrder() + 1;

            data = new GoodsCategory();
            data.setOrderNum(maxOrderNum);
        } else {
            boolean cnt = goodsCategoryService.getSameNameCnt(id, name);
            if (cnt) return R.fail("种类名称已存在");
            // 修改
            data = goodsCategoryService.getById(id);
            if (null == data) return R.fail("获取失败");
        }
        if (name != null)
            data.setName(name);
        if (mallStatus != null)
            data.setMallStatus(mallStatus);
        if (enabled != null)
            data.setEnabled(enabled);
//        data.setFreeDeliveryId(freeDeliveryId);
        goodsCategoryService.saveOrUpdate(data);

        // 清除商城商品种类
        redisService.setNullLike(RedisParam.MALL_CATEGORY);
        return R.ok();
    }


    @ApiOperation(value = "修改启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "启用状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        this.goodsCategoryService.updateEnabled(Lists.newArrayList(id), enabled);

        // 清除商城商品种类
        redisService.setNullLike(RedisParam.MALL_CATEGORY);
        return R.ok();
    }


    @ApiOperation(value = "获取商品种类详情")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        GoodsCategory category = goodsCategoryService.getById(id);
        if (category == null) return R.fail("获取失败");

        return R.ok(category);
    }


    @ApiOperation(value = "删除商品种类")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        long count = goodsCategoryLogService.getByCategoryId(id);
        if (count > 0)
            return R.fail("已被商品使用，无法删除");

        goodsCategoryService.removeById(id);

        // 清除商城商品种类
        redisService.setNullLike(RedisParam.MALL_CATEGORY);
        return R.ok();
    }


    @ApiOperation(value = "上移/下移")
    @PostMapping(value = "/upOrDown", produces = "application/json; charset=UTF-8")
    public R<Object> upOrDown(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "类型 1.上移 2.下移") @RequestParam(value = "type") Integer type
    ) {
        GoodsCategory data = goodsCategoryService.getById(id);
        if (data == null) return R.fail("分类获取失败");
        if (type.equals(1)) {
            // 上移
            GoodsCategory preData = goodsCategoryService.getLtShowOrder(data.getOrderNum());
            if (preData == null) return R.fail("已在最前");
            // 和前一条记录的序号互换
            Integer prevOrderNum = preData.getOrderNum();
            Integer curOrderNum = data.getOrderNum();
            // 序号互换
            goodsCategoryService.updateOrderNum(preData.getId(), curOrderNum);
            goodsCategoryService.updateOrderNum(data.getId(), prevOrderNum);
        } else if (type.equals(2)) {
            // 下移
            GoodsCategory backData = goodsCategoryService.getGtShowOrder(data.getOrderNum());
            if (backData == null) return R.fail("已在最后");
            // 和后一条记录的序号互换
            Integer backOrderNum = backData.getOrderNum();
            Integer curOrderNum = data.getOrderNum();
            // 序号互换
            goodsCategoryService.updateOrderNum(backData.getId(), curOrderNum);
            goodsCategoryService.updateOrderNum(data.getId(), backOrderNum);
        } else {
            return R.fail("类型错误");
        }
        return R.ok();
    }

    /**
     * 设置包邮模板时，判断商品种类下商品的其他商品种类的包邮模板是否一致
     */
//    private String judgeGoodsCategoryFreeDeliverySame(Long categoryId, Long freeDeliveryId) {
//        // 根据商品种类id模糊查询商品
//        List<Goods> goodsList = goodsService.listByCategoryIdLike(categoryId);
//
//        if (CollectionUtils.isNotEmpty(goodsList)) {
//            // 去重重复的商品种类id
//            Set<Long> categoryIdList = new HashSet<>();
//            for (Goods goods : goodsList) {
//                String categoryIds = goods.getCategoryIds();
//                if (StringUtils.isNotBlank(categoryIds)) {
//                    categoryIdList.addAll(OrderFormV3Util.getCategoryIdList(categoryIds));
//                }
//            }
//
//            // 将本商品种类id删除
//            categoryIdList.remove(categoryId);
//
//            // 根据其他的商品种类id获取包邮模板
//            if (CollectionUtils.isNotEmpty(categoryIdList)) {
//                List<GoodsCategory> otherCategoryList = goodsCategoryService.listByIdIn(categoryIdList);
//                if (CollectionUtils.isNotEmpty(otherCategoryList)) {
//                    String msg = "";
//                    for (GoodsCategory goodsCategory : otherCategoryList) {
//                        if (goodsCategory.getFreeDeliveryId() != null && !goodsCategory.getFreeDeliveryId().equals(freeDeliveryId)) {
//                            String freeDeliveryName = StringUtils.isNotBlank(goodsCategory.getFreeDeliveryName()) ? goodsCategory.getFreeDeliveryName() : "未设置包邮模板";
//                            msg += goodsCategory.getName() + "-" + freeDeliveryName + "；";
//                        }
//                    }
//                    return msg;
//                }
//            }
//        }
//
//        return "";
//    }

}
